/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.web.servlet.admin.menu;

import com.supinfo.youfood.entity.Employe;
import com.supinfo.youfood.entity.Menu;
import com.supinfo.youfood.service.MenuService;
import com.supinfo.youfood.service.ProduitService;
import com.supinfo.youfood.service.RestaurantService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author TaKeO
 */
@WebServlet(name = "ListMenuAdminServlet", urlPatterns = {"/admin/liste-menus"})
public class ListMenuAdminServlet extends HttpServlet {
    
    @EJB
    private MenuService menuService;
    
    @EJB
    private ProduitService produitService;
    
    @EJB
    private RestaurantService restoService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        Employe employe = (Employe) request.getSession().getAttribute("currentEmp");
        if(employe.getType_employe().getDroits() < 700) 
        {
            request.setAttribute("isAdmin", false);
        }
        else request.setAttribute("isAdmin", true);
        
        List<Menu> menus;
        
        List<Menu> menusResto = new ArrayList();
        List<Menu> menusDeChaine = new ArrayList();
        if(employe.getType_employe().getDroits() < 700)
        {
            if(request.getParameter("id")!=null)
            {
                menus = produitService.findProduitById(new Long(request.getParameter("id"))).getMenus();
                
            }
            else menus = menuService.getAllActifMenus();
            
            for(int i=0; i<menus.size(); i++)
            {
                if(menus.get(i).getActif() == 1)
                {
                    if(restoService.findRestoById(employe.getResto().getId_restaurant()).getMenus().contains(menus.get(i)))
                    {
                        menusResto.add(menus.get(i));
                    }
                    else
                    {
                        menusDeChaine.add(menus.get(i));
                    }
                }
            }
            request.setAttribute("menus", menusResto);
            request.setAttribute("menusDeChaine", menusDeChaine);
        }
        else
        {
            if(request.getParameter("id")!=null)
            {
                menus = produitService.findProduitById(new Long(request.getParameter("id"))).getMenus();
            }
            else menus = menuService.getAllMenus();
            request.setAttribute("menus", menus);
        }
        
        request.getRequestDispatcher("/jsp/admin/liste-menus.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        
    }

}
